/*
 * @(#)MainPanel.java
 * 
 * Copyright(C) 2004 Yujin Kim
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.netexplode.jtunes.client.ui.component;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JPanel;

import com.netexplode.jtunes.model.player.PlayerManager;

/**
 * <code>MainPanel</code> represents .....
 * 
 * @author ykim
 * @version $Revision: 1.9 $
 * @since 0.1
 */
public class MainPanel extends JPanel {

	private static final long serialVersionUID = 1L;

	private static final Component FILLER = Box.createRigidArea(new Dimension(12, 12));
	
	private boolean initialized;

	private AlbumArtPanel albumArtPanel;

	private LcdPanel lcdPanel;

	private SourceDevicePanel sourceDevicePanel;

	private LibraryPanel libraryPanel;

	private FullMenuBar fullMenuBar;

	private ControllerPanel controllerPanel;

	private SearchPanel searchPanel;

	private BrowseButton browseButton;

	private PlayerOptionBar playerOptionBar;

	private CollectionInfoLabel collectionInfoLabel;

	private DeviceControlBar deviceControlBar;

	private PlayerManager playerManager;

	public void setPlayerManager(PlayerManager playerManager) {
		this.playerManager = playerManager;
	}

	public void setAlbumArtPanel(AlbumArtPanel albumArtPanel) {
		this.albumArtPanel = albumArtPanel;
	}

	public void setLcdPanel(LcdPanel lcdPanel) {
		this.lcdPanel = lcdPanel;
	}

	public void setLibraryPanel(LibraryPanel libraryPanel) {
		this.libraryPanel = libraryPanel;
	}

	public void setSourceDevicePanel(SourceDevicePanel sourceDevicePanel) {
		this.sourceDevicePanel = sourceDevicePanel;
	}

	public void setBrowseButton(BrowseButton browseButton) {
		this.browseButton = browseButton;
	}

	public void setCollectionInfoLabel(CollectionInfoLabel collectionInfoLabel) {
		this.collectionInfoLabel = collectionInfoLabel;
	}

	public void setControllerPanel(ControllerPanel controllerPanel) {
		this.controllerPanel = controllerPanel;
	}

	public void setDeviceControlBar(DeviceControlBar deviceControlBar) {
		this.deviceControlBar = deviceControlBar;
	}

	public void setFullMenuBar(FullMenuBar fullMenuBar) {
		this.fullMenuBar = fullMenuBar;
	}

	public void setPlayerOptionBar(PlayerOptionBar playerOptionBar) {
		this.playerOptionBar = playerOptionBar;
	}

	public void setSearchPanel(SearchPanel searchPanel) {
		this.searchPanel = searchPanel;
	}
	
	

	public boolean isInitialized() {
		return this.initialized;
	}

	public void init() {
		this.setOptions();
		super.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

		/*
		 * file menu bar
		 */
		fullMenuBar.setMaximumSize(new Dimension(Integer.MAX_VALUE, 2));
		super.add(fullMenuBar);

		/*
		 * top
		 */
		final JPanel top = new JPanel();
		top.setLayout(new BoxLayout(top, BoxLayout.LINE_AXIS));
		top.add(FILLER);
		top.add(controllerPanel);
		top.add(lcdPanel);
		top.add(searchPanel);
		top.add(browseButton);
		top.add(FILLER);
		top.setMaximumSize(new Dimension(Integer.MAX_VALUE, FILLER.getHeight()));
		super.add(top);

		/*
		 * middle
		 */
		// create left (source device + album art)
		final JPanel left = new JPanel();
		left.setLayout(new BoxLayout(left, BoxLayout.PAGE_AXIS));

		sourceDevicePanel.setPreferredSize(new Dimension(200, 100));
		sourceDevicePanel.setMaximumSize(new Dimension(300, Integer.MAX_VALUE));
		left.add(sourceDevicePanel);

		final JPanel art = new JPanel();
		// albumArtPanel.setSize(new Dimension(100, 100));
		albumArtPanel.setPreferredSize(new Dimension(180, 180));
		albumArtPanel.setMaximumSize(new Dimension(180, 180));
		art.add(albumArtPanel, BorderLayout.CENTER);

		left.add(albumArtPanel);

		// create splitter using left and right
		final JPanel middle = new JPanel();
		middle.setLayout(new BoxLayout(middle, BoxLayout.LINE_AXIS));
		middle.add(left);
		// create right (collection table)
		middle.add(libraryPanel);

		// final JSplitPane middle = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
		// left, right);
		// middle.setLayout(new BoxLayout(middle, BoxLayout.LINE_AXIS));

		// set border
		middle.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(""), BorderFactory
				.createEmptyBorder(1, 1, 1, 1)));
		// set background
		middle.setBackground(Color.GRAY);
		super.add(middle);

		/*
		 * bottom
		 */
		final JPanel bottom = new JPanel();
		bottom.setLayout(new BoxLayout(bottom, BoxLayout.LINE_AXIS));
		bottom.add(new PlayerOptionBar());
		bottom.add(new CollectionInfoLabel());
		bottom.add(new DeviceControlBar());
		bottom.setMaximumSize(new Dimension(Integer.MAX_VALUE, 2));
		super.add(bottom);

//		if (playerManager.isInitialized()) {
//			jTunes.setLcdPanelInfo();
//		}
		
		this.initialized = true;
	}

	private void setOptions() {
		super.setFont(new Font("Verdana", Font.PLAIN, 10));
		super.setAlignmentX(LEFT_ALIGNMENT);
	}

	/* REFRESH METHODS */

	public void refreshLibraryPanel() {
		libraryPanel.refresh();
	}

	public void refreshSharedCollectionPanel() {
		libraryPanel.reloadShared();
	}

	public void refreshLocalCollectionPanel() {
		libraryPanel.reloadLocal();
	}

	public void setAlbumArtPanelVisible(boolean visible) {
		albumArtPanel.setVisible(visible);
	}

	public void setLcdPanelText(String text) {
		lcdPanel.setLcdPanelText(text);
	}

	public void refreshTree() {
		sourceDevicePanel.refresh();
	}
}